//
//  HomeTableViewController.swift
//  MicroBlog
//
//  Created by Apple on 16/9/19.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
import YYModel
//重用标记
private let HomeTableViewControllerIdentifier = "cell"
class HomeTableViewController: HMVisitorTableViewController {
    
    //懒加载viewModel
    fileprivate lazy var statusListViewModel: HMStatusListViewModel = HMStatusListViewModel()
    //上拉加载
    fileprivate lazy var pullUpIndicatorView: UIActivityIndicatorView = {
        let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
        view.color = UIColor.darkGray
        return view
    }()
    //下拉刷新
    fileprivate lazy var pullDownControl: HMRefreshControl = {
        let view = HMRefreshControl()
        view.addTarget(self, action: #selector(pullDownRefreshing), for: .valueChanged)
        return view
    }()
    //下拉刷新
    fileprivate lazy var pullDownView: UIRefreshControl = {
        let view = UIRefreshControl()
        view.addTarget(self, action: #selector(pullDownRefreshing), for: .valueChanged)
        return view
    }()
    
    //tipLabel
    fileprivate lazy var tipLabel: UILabel = {
        let label = UILabel(textColor: UIColor.white, fontSIze: 12)
        label.backgroundColor = UIColor.orange
        label.textAlignment = .center
        label.isHidden = true
        return label
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !isLogin {
            visitorView?.updataVisitorViewInfo(message: nil, imageName: nil)
        } else {
            setupUI()
            //登录成功才加载数据
            loadData()
            setupTableView()
        }
    }
    
    //添加控件,设置约束
    private func setupUI() {
        if let nav = self.navigationController {
            //tipLabel添加到导航栏下面
            navigationController?.view.insertSubview(tipLabel, belowSubview: nav.navigationBar)
            //设置与导航栏底部对齐
            tipLabel.frame = CGRect(x: 0, y: nav.navigationBar.frame.maxY - 35, width: nav.navigationBar.width, height: 35)
        }
    }
    //设置UITableView的相关操作
    private func setupTableView() {
        tableView.register(HomeTableViewCell.self, forCellReuseIdentifier: HomeTableViewControllerIdentifier)
        //自动计算行高
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 200
        //去掉分割线
        tableView.separatorStyle = .none
        //上拉加载
        tableView.tableFooterView = pullUpIndicatorView
        //下拉刷新,自定义类
        //        tableView.addSubview(pullDownView)
        tableView.addSubview(pullDownControl)
    }
    //结束刷新
    private func endRefreshing() {
        pullUpIndicatorView.stopAnimating()
        pullDownView.endRefreshing()
        //自定义下拉刷新
        pullDownControl.endRefreshing()
    }
    
    //加载首页微博数据
    func loadData() {
        statusListViewModel.loadData(isPullup: pullUpIndicatorView.isAnimating) { (isTrue, message) in
            //上拉加载不开启动画
            if !self.pullUpIndicatorView.isAnimating{
                self.startAnimation(message: message)
            }
            //加载数据之后结束
            self.endRefreshing()
            if isTrue{
                self.tableView.reloadData()
            }
        }
    }
    //开启tip动画
    private func startAnimation(message: String) {
        //开始动画的时候不再开启动画
        if tipLabel.isHidden == false {
            return
        }
        tipLabel.text = message
        tipLabel.isHidden = false
        UIView.animate(withDuration: 1, animations: {
            self.tipLabel.transform = CGAffineTransform(translationX: 0, y: self.tipLabel.height)
        }) { (_) in
            UIView.animate(withDuration: 1, animations: { 
                self.tipLabel.transform = CGAffineTransform.identity
                }, completion: { (_) in
                    self.tipLabel.isHidden = true
            })
        }
    }
    
    //下拉刷新
    @objc private func pullDownRefreshing() {
        loadData()
    }
}
//数据源方法
extension HomeTableViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: HomeTableViewControllerIdentifier, for: indexPath) as! HomeTableViewCell
        cell.statusViewModel = statusListViewModel.statusList[indexPath.row]
        //取消选中
        cell.selectionStyle = .none
        return cell
    }
    //将要显示cell的时候判断最后一个cell
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        //最后一个cell并且没有加载的时候
        if indexPath.row == statusListViewModel.statusList.count - 1 && !pullUpIndicatorView.isAnimating{
            pullUpIndicatorView.startAnimating()
            loadData()
        }
    }
}
